// delay for given number of microseconds (use 1-1000) or milliseconds (1-65535) using timer 4
// (C) Copyright 2009, 2010 Communications Programming, Inc.

#include "userLib.h"

// TimerTicksPerMicrosec defined in picInit()

extern UINT16 TimerTicksPerMicrosec;// for use in TMRx and other timer funcs eg delayTx

void delayT4_init()					// tpus = number of timer ticks for each microsecond
{
	T4CON = T4CON_VAL; 				// timer on, set prescale
	PR4 = 0xFFFF;					// no reset
}

void delayT4_us( UINT16 us )		// delay a given number of microseconds
{									// use for us values < 1000 so that loop counter not blown
	us *= TimerTicksPerMicrosec;	// apply in param multiplier
	TMR4 = 0;						// reset timer value
	while( TMR4 < us )
		{}
}

void delayT4_ms( UINT16 ms )		// delay a given number of milliseconds (1-65535)
{
	while( ms-- )
		delayT1_us(1000);			// each millisec = 1000 microsecs
}

